import { getCategory,getGoodCatesList } from "../../api";

import { baseImgUrl } from "../../setting";
const app = getApp();
Page({
  data: {
    activeTab: 0,
    tabs: [{anchor:51,image:"20220723153135564750.jpg",sub:[],name:'品牌手机'}],
    showTabs: true
  },
  async onLoad(option) {
    this.setData({
      parentid:option.url
    })
    
     await this.getList();
    // const launchOptions = my.getLaunchOptionsSync();
    // if(launchOptions.query&&launchOptions.query.id&&launchOptions.query.id!='undefined'&&launchOptions.query.id!='null'){
    //   this.handleChange(launchOptions.query.id);
    // }
    
  },
  onShow() {
    this.setData({
      showTabs: false
    })
    setTimeout(() => {
      this.setData({
        showTabs: true
      })
    }, 100);
    const { cateId } = app.globalData
    if(cateId){
      this.handleChange(cateId);
    }
  },
  // 获取分类列表
  getList() {
    const { parentid } = this.data
    getCategory({
      parentid:parentid||''
    }).then(res => {
      if (res) {
        const list = res.map(k => {
          const items = k.sub ? k.sub.map(p => {
            return { text: p.name, id: p.id, icon: baseImgUrl + p.image }//子级
          }) : [];
          return { title: k.name, anchor: k.id, image: k.image, sub: items }//父级
        });
        this.setData({
          tabs: list
        })
      }
      this.getProductList(); 
    })

  },
    getProductList() {
      const index1 = this.data.activeTab;
      const parentid = this.data.tabs[index1].anchor;
      getGoodCatesList({   parentid:parentid||'' }).then(res => {
        my.stopPullDownRefresh()
        let newArr = []
        for (let key in res.data) {
          newArr.push(res.data[key])
        }
     
        newArr.forEach(item => {
          if (item.rent_label) {
            if (item.rent_label.indexOf("，") == 0) {
              let itemLable = item.rent_label.substr(1)
              if (itemLable.indexOf("，") == -1) {
                item['label1'] = itemLable
              } else {
                item.rent_label = itemLable.split('，')
                item['label1'] = item.rent_label[0]
                item['label2'] = item.rent_label[1]
                item['label3'] = item.rent_label[2]
              }
            } else {
              item.rent_label = item.rent_label.split(',')
              item['label1'] = item.rent_label[0]
              item['label2'] = item.rent_label[1]
              item['label3'] = item.rent_label[2]
            }
          }
        })
        this.setData({
          productList: newArr || []
        });
      }).catch(err =>{
        my.stopPullDownRefresh();
      })
    },

  onItemClick(ev) {
    const index1 = this.data.activeTab;
    const index2 = ev.detail.index;
    const cateId = this.data.tabs[index1].sub[index2].id;
    my.navigateTo({ url: '../../pagesA/pages/productList/productList?cateId=' + cateId });
  },
  handleTabClick(index) {
    this.setData({
      activeTab: index,
    });
    this.getProductList();
  },
  handleChange(index) {
    this.setData({
      activeTab: index,
    });
    this.getProductList();

  },
  onChange(index) {
    this.setData({
      activeTab: index,
    });
    this.getProductList();
  },
  goGoodsDetail(e) {
    my.navigateTo({
      url: '../../pagesA/pages/productDetails/productDetails?goodsId=' + e.currentTarget.dataset.id+'&sale_user_id='+app.globalData.sale_user_id
    })
  },
  goToSearch() {
    my.navigateTo({ url: '/pagesA/pages/search/search' });
  }
});
